<?php
namespace app\api\controller\robat;

use app\api\controller\Base;
use app\api\controller\mytool\Mytool;
use app\common\model\orderdata\Goods as Goodsmodel;
use app\common\model\company\ContractDetails;
use think\Controller;
use think\facade\Cache;

class Goods extends Base
{
    /**
     * 根据合同内的危废代码和名称 完善系统中危废的名字
     * @return [type] [description]
     */
    public function contract_goodname_perfect(){
        $data = ContractDetails::field('GoodId,GoodName')->select();
        $id = [];
        $name = [];
        foreach ($data as $key => $value) {
            $id[] = $value['GoodId'];
            $name[] = $value['GoodName'];
        }
        Goodsmodel::NamePerfect($id,$name);
        return myok('完善成功');
    }
    /**
     * 将没有定位的企业获取定位
     * @return [type] [description]
     */
    public function perfect()
    {
        $data = input('data');
        if (empty($data)) {
            return myerr('数据为空');
        }
        $data = json_array($data, true);
        if (empty($data['obj']['entitys'])) {
            return myerr('缺少数据');
        }
        $value = $data['obj']['entitys'][0];
        $err = '';
        foreach ($data['obj']['entitys'] as $key => $value) {
            $sb = Goodsmodel::where('OrderNumber', $value['wasteCode'])->find();
            if (empty($sb)) {
                $err  =  $err .'第' . ($key + 1) . '项没有找到'."\n";
                continue;
                // return myerr('第' . ($key + 1) . '项没有找到');
            }
            //启用状态
            $sb->State = Goodsmodel::STATEOK;
            
            //有害成分
            $value['harmfulComponents'] =  trim($value['harmfulComponents']);
            $HarmfulList = $sb->getData('HarmfulList');
            if (empty($HarmfulList)) {
                $sb->HarmfulList = $value['harmfulComponents'];
            } else {
                if (mb_strpos($HarmfulList, $value['harmfulComponents']) === false) {
                    //没有找到
                    $sb->HarmfulList = $HarmfulList.','.$value['harmfulComponents'];
                }
            }

            //危废名
            $value['name'] = trim($value['name']);
            $AnotherName = $sb->getData('AnotherName');
            if (empty($AnotherName)) {
                $sb->AnotherName = $value['name'];
            } else {
                if (mb_strpos($AnotherName, $value['name']) === false) {
                    //没有找到
                    $sb->AnotherName = $AnotherName.','.$value['name'];
                }
            }
            $sb->save();
        }
        if(!empty($err)){
            echo '错误列表'.$err ;
        }
        echo "\n"."操作完成";
        return;
    }

    /**
     * 将相关数据导出
     * 废物类别及名称（HW31含铅废物） 危废代码（900-052-31XXXX） 废物名称（废铅蓄电池） 计量单位（吨） 废物类别(库存初始化) 
     * /*废物形态（固态）/ /*危险特征（腐蚀性 毒性）/ 有害物质名称（铅酸） /*危险情况（吞食后会中毒）/ 应急措施（穿上适当的防护服，戴上） 应急设备
     * @return [type] [description]
     */
    public function input(){
        $data = input('data');
        if (empty($data)) {
            return myerr('数据为空');
        }
        $data = json_array($data, true);
        if (empty($data['obj']['entitys'])) {
            return myerr('缺少数据');
        }
        $value = $data['obj']['entitys'][0];
        $err = '';
        $ndata = [];
        foreach ($data['obj']['entitys'] as $key => $value) {
            $sb = Goodsmodel::where('OrderNumber', $value['wasteCode'])->find();
            if (empty($sb)) {
                $err  =  $err .'第' . ($key + 1) . '项没有找到'."\n";
                continue;
                // return myerr('第' . ($key + 1) . '项没有找到');
            }
            //危废名
            $value['name'] = trim($value['name']);
            $AnotherName = $sb->getData('AnotherName');
            if (!empty($AnotherName)&&mb_strpos($AnotherName, $value['name']) === true) {
                //相同
                continue;
            }
           
            $nndata = [];
            $nndata['Type'] = $value['wasteType'];
            $nndata['OrderNumber'] = $value['wasteCode'];
            $nndata['Name'] = $value['name'];
            $nndata['unitName'] = $value['unitName'];
            $nndata['productType'] = $value['productType'];
            $nndata['harmfulComponents'] = $value['harmfulComponents'];
            $nndata['emergencyEquipment'] = $value['emergencyEquipment'];
            $nndata['emergencyMeasure'] = $value['emergencyMeasure'];
            if($sb['State']==Goodsmodel::STATEOK){
                $ndata[] = $nndata;
            }
        }
        $head = ['废物类别及名称','危废代码','废物名称','计量单位（吨）','废物类别','有害物质','应急设备','应急措施'];
        $key = ['Type','OrderNumber','Name','unitName','productType','harmfulComponents','emergencyEquipment','emergencyMeasure'];
        if(!empty($err)){
            echo '错误列表'.$err ;
        }
        // echo "\n"."操作完成"."\n";
        // echo(json_encode($ndata));
        // dump($ndata);
        Cache::set('excel',['危废代码对应表',$ndata,$head,$key],120);
        echo "\n"."操作完成，请在2分钟内访问下载"."\n";
        return;
    }

    /**
     * 输出文件
     * @return [type] [description]
     */
    public function output(){
        $data = Cache::get('excel');
        if(!empty($data)){
            return Mytool::exceloutdata($data[0],$data[1],$data[2],$data[3]);
        }else{
            echo '没有找到您需要的文件';
            return ;
        }
    }
}
